{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c1c469e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "以下为可办理的业务：\n",
      "        1. 查询资产\n",
      "        2. 存款\n",
      "        3. 取款\n",
      "        4. 计算复利\n",
      "        5. 计算贷款\n",
      "        6. 计算未来资产\n",
      "        q. 退出\n",
      "请输入您要办理的业务代码: 1\n",
      "你的资产总额为：0.00\n",
      "你的资产明细为：\n",
      "\n",
      "存款：0.00\n",
      "理财：0.00\n",
      "负债：0.00\n",
      "请输入您要办理的业务代码: 2\n",
      "请输入存款金额: 10000\n",
      "已存款：10000.00 元\n",
      "当前余额：10000.00 元\n",
      "请输入您要办理的业务代码: q\n",
      "欢迎下次光临！再见！\n"
     ]
    }
   ],
   "source": [
    "means = [0, 0, 0]  \n",
    "loan = 0\n",
    "rate = 0\n",
    "pay = 0\n",
    "investment = 0\n",
    "annual_rate = 0\n",
    "\n",
    "\n",
    "# 计算定投预期收益\n",
    "# 定投收益的计算公式为：M=a(1+x)[-1+(1+x)^n]/x;\n",
    "# 其中M代表预期收益，a代表每期定投金额，x代表收益率，而n代表定投期数。\n",
    "# 假设用户每月定投金额为300元，一年也就是3600元，年收益率为15%，\n",
    "# 定投期限为35年，则可以计算出收益为3600(1+15%)[-1+(1+15%)^35]/15%=3648044元。\n",
    "def fixed_investment(inv, a_rate, y):\n",
    "    global means\n",
    "    inv = 12 * inv\n",
    "    a_rate = a_rate / 100\n",
    "    if a_rate == 0:\n",
    "        expected = 0\n",
    "    else:\n",
    "        expected = inv * (1 + a_rate) * (pow((1 + a_rate), y) - 1) / a_rate\n",
    "    print(\"定投的预期收入为: %.2f\" % expected)\n",
    "    means[1] = expected\n",
    "    return expected\n",
    "\n",
    "\n",
    "def balance():\n",
    "    total = 0\n",
    "    for i in means:\n",
    "        total += i\n",
    "    print(\"你的资产总额为：%.2f\" % total)\n",
    "    print(\"你的资产明细为：\\n\")\n",
    "    print(\"存款：%.2f\" % means[0])\n",
    "    print(\"理财：%.2f\" % means[1])\n",
    "    print(\"负债：%.2f\" % means[2])\n",
    "\n",
    "\n",
    "def saving(amount):\n",
    "    global means\n",
    "    if amount < 0:\n",
    "        print(\"存款金额不可小于 0！\")\n",
    "    else:\n",
    "        means[0] += amount\n",
    "        print(\"已存款：%.2f 元\" % amount)\n",
    "        print(\"当前余额：%.2f 元\" % means[0])\n",
    "\n",
    "\n",
    "def draw_money(drawing):\n",
    "    global means\n",
    "    if drawing < 0:\n",
    "        print(\"取款金额不可小于 0！\")\n",
    "    elif drawing > means[0]:\n",
    "        print(\"取款金额不可超过余额！\")\n",
    "    else:\n",
    "        means[0] -= drawing\n",
    "        print(\"已取款： %.2f 元\" % drawing)\n",
    "        print(\"当前余额： %.2f 元\" % means[0])\n",
    "\n",
    "\n",
    "def loans(loan, rate, pay, years):\n",
    "    global means\n",
    "    if pay < (loan - pay) * rate:\n",
    "        print(\"你是还不完的！！！\")\n",
    "    else:\n",
    "        if years == 0:\n",
    "            count = 0\n",
    "            while loan > 0:\n",
    "                loan -= pay\n",
    "                loan *= (1 + rate)\n",
    "                count += 1\n",
    "            print(\"将在 %d 年后还完贷款。\" % count)\n",
    "        else:\n",
    "            for _ in range(years):\n",
    "                loan -= pay\n",
    "                if loan == 0:\n",
    "                    break\n",
    "                else:\n",
    "                    loan *= (1 + rate)\n",
    "                    print(\"你现在的负债是: %.2f\" % loan)\n",
    "            # means[2] = loan\n",
    "            return loan\n",
    "\n",
    "\n",
    "# 未来财务状况\n",
    "def future(years):\n",
    "    income = fixed_investment(investment, annual_rate, years)\n",
    "    debt = loans(loan, rate, pay, years)\n",
    "    captial = means[0] + income - debt\n",
    "    print(\"你第%i年的总资产有: %.3f\" % (years, captial))\n",
    "\n",
    "\n",
    "def init():\n",
    "    print()\n",
    "    print('''以下为可办理的业务：\n",
    "        1. 查询资产\n",
    "        2. 存款\n",
    "        3. 取款\n",
    "        4. 计算复利\n",
    "        5. 计算贷款\n",
    "        6. 计算未来资产\n",
    "        q. 退出''')\n",
    "\n",
    "\n",
    "def main():\n",
    "    init()\n",
    "    while True:\n",
    "        choice = input(\"请输入您要办理的业务代码: \")\n",
    "        #  查询余额\n",
    "        if choice == \"1\":\n",
    "            balance()\n",
    "        # 存款\n",
    "        elif choice == \"2\":\n",
    "            inc = float(input(\"请输入存款金额: \"))\n",
    "            saving(inc)\n",
    "        # 取款\n",
    "        elif choice == \"3\":\n",
    "            dec = float(input(\"请输入取款金额: \"))\n",
    "            draw_money(dec)\n",
    "        # 计算定投\n",
    "        elif choice == \"4\":\n",
    "            investment = float(input(\"请输入每月定投金额: \"))\n",
    "            annual_rate = float(input(\"请输入年收益率: \"))\n",
    "            years = int(input(\"请输入定投期限(年): \"))\n",
    "            if investment <= 0 or annual_rate <= 0 or years <= 0:\n",
    "                print(\"输入的数据有误\")\n",
    "            else:\n",
    "                money = fixed_investment(investment, annual_rate, years)\n",
    "            print(\"最终收获: %.2f 元\" % money)\n",
    "        # 计算贷款\n",
    "        elif choice == \"5\":\n",
    "            loan = float(input(\"请输入当前贷款: \"))\n",
    "            rate = float(input(\"请输入年利率: \"))\n",
    "            pay = float(input(\"请输入每年还款: \"))\n",
    "            if loan <= 0 or rate <= 0 or pay <= 0:\n",
    "                print(\"输入的数据有误\")\n",
    "            else:\n",
    "                loans(loan, rate, pay, 0)\n",
    "        elif choice == \"6\":\n",
    "            years = int(input(\"希望查询多少年后的财务状况? \"))\n",
    "            future(years)\n",
    "        # 退出\n",
    "        elif choice == \"q\":\n",
    "            print(\"欢迎下次光临！再见！\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"你输入的指令有误，请重新输入\\n\")\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
